<!DOCTYPE html>

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <title>Server Side Events - Raxan User Guide</title>
    <link href="../raxan/ui/css/master.css" rel="stylesheet" type="text/css" />
    <!--[if lt IE 8]> <link rel="stylesheet" href="../raxan/ui/css/master.ie.css" type="text/css"><![endif]-->
    <link href="../raxan/ui/css/default/theme.css" rel="stylesheet" type="text/css" />
    <link href="style.css" rel="stylesheet" type="text/css" />
    <link href="highlight/styles/default.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="highlight/highlight.js"></script>
    <script type="text/javascript">
        <!--
        hljs.initHighlightingOnLoad('javascript','html','php','css');
        //-->
    </script>
    <!--[if lt IE 7]>
        <style type="text/css"> form input.textbox { height: 26px; }</style>
    <![endif]-->
</head>

<body>
    <div class="container">
        <div id="header" class="rax-header-pal rax-metalic">
            <h2 class="ltm bottom c14">Raxan User Guide</h2>
            <ul>
                <li><a href="../../index.html">Home</a></li>
                <li><a href="index.html">Overview</a></li>
                <li><a href="features.html">Features</a></li>
                <li><a href="table-of-contents.html" title="Table of Content">Contents</a></li>
                <li><a href="../examples">Examples</a></li>
            </ul>
            <ul class="search">
                <li>
                    <form class="tpm c9" name="form1" action="../tools/search.php" method="get">
                        <input class="c6 textbox round left" placeholder="Search" type="text" name="q" value="" title="Search User Guide"  />
                        <input class="c2 button round left ltm" type="submit" value="Go" />
                    </form>
                </li>
            </ul>

        </div>
        <hr class="space"/>
        <div class="master-content-wrapper">
            <div class="container prepend-top c48 master-content"><h2>Server-Side Events</h2>

<h3>Client-Server Event Binding</h3>

<p>Client-Server event binding is what makes Raxan truly special. Imagine never having to write a single line of JavaScript code
in order to receive an Ajax notification when a user clicked on a button. Imagine being able to bind your server-side event handler to just about
any HTML tag inside your web page. The possibilities are endless when you realize that you have total control over every DOM element inside your
client's web browser.</p>

<h3>Binding to an Event</h3>

<p>Sending data to and from the server requires very little or no JavaScript coding when an event is triggered within a browser. In fact,
it is very straightforward to configure your web pages to receive an server-side event notifications from the client .</p>

<p>The simplest way to do this is to use the xt-bind extended attribute to listen to events being triggered from the client.</p>

<p>Usage:</p>

<pre><code>xt-bind="type, callback, serialize, autoDisable, autoToggle"
</code></pre>

<ul>
<li><strong>type</strong> - The type of event to be invoked on the server.</li>
<li><strong>callback</strong> - Server side callback method. The method name must be a class method within the page.</li>
<li><strong>serialize</strong> - CSS selector - Form elements that are to be serialized when sending data to the server.</li>
<li><strong>autoDisable</strong> - CSS selector - Disables the selected elements during event transmission. Set to true to disable the current element</li>
<li><strong>autoToggle</strong> - CSS selector - Show/Hide selected elements during event transmission. Set to true to toggle the current element</li>
</ul>

<p><strong>Note:</strong> Comma separate CSS selectors (e.g .class1, .class2, .classN) are currently not supported when using the xt-bind attribute. They can
however be used with the <strong>bind()</strong> method.</p>

<p>Usage Examples:</p>

<pre><code>xt-bind="click, showDetails" - makes a synchronous call to the server

xt-bind="#click, showDetails"  - makes an asynchronous call to the server

- The following will disable the current element, toggle the "#loader" element and serialize
- the all "form" elements before making an asynchronous post back to the server

xt-bind="#click,editUser,form,true,#loader"
</code></pre>

<p>Working example:</p>

<pre><code class="php">&lt;?php
    require_once('raxan/pdi/autostart.php');

    class MyPage extends RaxanWebPage {

        // callback function
        protected function buttonClick($e) {
            // select the #msg element and set html to hello world
            $this-&gt;msg-&gt;html('Hello World');
        }
    }

?&gt;

&lt;form name="form1" action="" method="post"&gt;
    &lt;input id="mybutton" type="button" value="Click Me" xt-bind="click,buttonClick" /&gt;
    &lt;div id="msg" /&gt;
&lt;/form&gt;
</code></pre>

<h3>Event Delegates</h3>

<p>Event delegates are special events that are used to bind an event handler to a dynamic set of elements. This eliminates the need of having to
individually bind each element to an event handler whenever the set is updated.</p>

<p>To do this you can use either the xt-delagate extended attribute or the</p>

<p>Usage:</p>

<pre><code>xt-delegate="selector type, callback, serialize, autoDisable, autoToggle"
</code></pre>

<ul>
<li><strong>selector</strong> - CSS Selector - The selected elements to bind (or delegate) the callback method</li>
<li><strong>type</strong> - The type of event (e.g. click) to be invoked on the server</li>
</ul>

<p><strong>Note:</strong> See xt-bind for a descrition of the other parameters</p>

<p>Usage Example:</p>

<pre><code>xt-delegate="a.edit click, editRecord"   - delegates a.edit click event
</code></pre>

<p>Working Example:</p>

<pre><code class="php">&lt;?php
    require_once('raxan/pdi/autostart.php');

    class NewPage extends RaxanWebPage {

        protected function doSomething($e) {
            $i = $e-&gt;intval(); // get the integer value for the event
            $this-&gt;panel-&gt;text('You have clicked item #'.$i);
        }
    }

?&gt;

&lt;ul xt-delegate="li a click,doSomething"&gt;
    &lt;li&gt;&lt;a href="#1"&gt;Item 1&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="#2"&gt;Item 2&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="#3"&gt;Item 3&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="#4"&gt;Item 4&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div id="panel"&gt;&lt;/div&gt;
</code></pre>

<p>The above will delegate the click event for all a.edit elements within for the current &lt;ul&gt; element. Newly added items that matches the a.edit
selector item will be automatically bounded to the click event handler.</p>

<h3>Timeout Events<a name="timeout"></a></h3>

<p>There are times when you will need to periodically update an element on a web page with content from the server without any user intervention.
This task would normally require a developer write JavaScript code to periodically poll the server for updates.</p>

<p>Raxan eliminates the need to have to manage your own client-side updates with the xt-autoupdate extended attribute:</p>

<p>Usage:</p>

<pre><code>xt-autoupdate = "true"  - Default mode. Enable automatic updates during ajax calls
xt-autoupdate = "seconds, callback, repeat, serialize, autotoggle" - enable timeouts
</code></pre>

<ul>
<li><strong>seconds</strong> - The number of seconds to wait before invoking the event.</li>
<li><strong>callback</strong> - Server side callback method. The method name must be a class method within the page.</li>
<li><strong>repeat</strong> - Sets number of times to repeat an Ajax timeout event. Setting the value to "true" or "repeat" will repeat the timeout indefinitely. Defaults to 1.</li>
<li><strong>serialize</strong> - CSS selector - Form elements that are to be serialized and posted to the server.</li>
<li><strong>autoToggle</strong> - CSS selector - Show/Hide selected elements during event transmission. Set to true to toggle the current element</li>
</ul>

<p>Note: Setting xt-autoupdate="true" will automatically cause the element to be updated during ajax calls. This is similar to calling the updateClient()
method.</p>

<p>Usage Examples:</p>

<pre><code>xt-autoupdate = "true"  - enable automatic updates during ajax calls
xt-autoupdate = "5000,update,true" - updates every 5 seconds (repeatedly)

- The following will asynchronously updates the element every
- 15 seconds (repeatedly) and toggles #loader during updates

xt-autoupdate = "#15000,update,true,,#loader"
</code></pre>

<p>Working Example:</p>

<pre><code class="php">&lt;?php
    require_once('raxan/pdi/autostart.php');

    class NewPage extends RaxanWebPage {

        protected function doSomething($e) {
            $this-&gt;msgbox-&gt;text('A message from your server');
        }
    }

?&gt;

&lt;a href="#" xt-bind="#click,doSomething"&gt;Click Here&lt;/a&gt;
&lt;div id="msgbox" xt-autoupdate="true"&gt;&lt;/div&gt;
</code></pre>

<h3>Setting Target Window</h3>

<p>It's possible to have the browser display your event response within a new window of frame. To do this, you can use the target or formtarget attribute
to set the window where the results will be displayed when the user triggers an event.</p>

<p>Example:</p>

<pre><code>&lt;a href="#" target="_blank" xt-bind="click,popup"&gt;New Window&lt;/a&gt;

&lt;input type="button" name="button1" id="button1" value="New Window" formtarget="_blank" xt-bind="click,popup" /&gt;

&lt;div target="_blank" xt-bind="click,popup"&gt;New Window&lt;/div&gt;
</code></pre>

<p>Target values:</p>

<ul>
<li><strong>_blank</strong> - load event response in a new blank window.</li>
<li><strong>_parent</strong> - load event response in the immediate parent of current document</li>
<li><strong>_self</strong> - load event response in the current window</li>
<li><strong>_top</strong> - load event response in the full body of the window. This useful for breaking out of a nested FRAMESET</li>
<li><strong>window name</strong> - Name of window or frame to load event response. For example: mywindow</li>
</ul>

<p><strong>Note:</strong> Target window will only work for full-page events.</p>

<h3>Using the bind() and delegate() methods</h3>

<p>The bind() method provides you with more options when compared to the xt-bind attribute. With the bind() and delegate() method you can use various event
callback options:</p>

<ul>
<li><p>Bind <strong>Function name</strong> to event. Call a user-defined function. Example:</p>

<pre><code class="php">&lt;?php
    $this-&gt;button1-&gt;bind('click', 'My_Function_Name');
?&gt;
</code></pre></li>
<li><p>Bind <strong>Page Method</strong> name to event. Call a method on the current page. Example:</p>

<pre><code class="php">&lt;?php
    $this-&gt;button1-&gt;bind('click', '.My_Page_Method');
?&gt;
</code></pre>

<p>Event callbacks are handled directly from the Page controller. When you bind an event using '.callback' the framework will automatically look
for the method on the page controller instance. In this case the method can be protected or public. The '.callback' format is only a shortcut to
array($this, 'callback').</p></li>
<li><p>Bind <strong>Object method</strong> to event:</p>

<pre><code class="php">&lt;?php
    $this-&gt;button1-&gt;bind('click',array($object, 'method_name'));
?&gt;
</code></pre>

<p>When binding an event to a method that's external to the page controller, you will have to use the array($object, 'method_name') format. In this
case method_name() must be a public method since it will be called from the page controller.</p></li>
<li><p>Bind <strong>Anonymous functions</strong> to event. This only works on <strong>PHP 5.3 and higher</strong>:</p>

<pre><code class="php">&lt;?php
    $this-&gt;button1-&gt;bind('click',function($e){
        // do something here
    });
?&gt;
</code></pre></li>
</ul>

<p>The bind() method can be configured to submit selected form content to the server or hide/show elements before or after an event is triggered:</p>

<pre><code class="php">&lt;?php

    $this-&gt;button1-&gt;bind('#click',array(
        'callback' =&gt; '.auto_search',
        'autoDisable' =&gt; true,   // disable the button during event call to server
        'autoToggle' =&gt; 'img#pre', // show pre loader
        'serialize' =&gt; 'form :input' // serialize and post form inputs back to the server
    ));

?&gt;
</code></pre>

<p>As stated above, the delegate() method is similar to the bind() method with the exception that it takes an additional css selector as the first parameter:</p>

<pre><code class="php">&lt;?php

    // delegate the click event for all a.edit element within list1
    $this-&gt;list1-&gt;delegate('a.edit', '#click', '.editRecord');

?&gt;
</code></pre>

<p><strong>Note:</strong> Event bindings must done before or during page load.</p>

<h4><strong>Bind options for events</strong></h4>

<ul>
<li><strong>callback</strong> - Server side callback function</li>
<li><strong>value</strong> - Value to be passed to the server-side event. This can be assigned from the client-side</li>
<li><strong>prefTarget</strong> - Sets the preferred target element to be invoked on the server. Format: target@url - @url is optional.</li>
<li><strong>targetWindow</strong> - Sets the target window to display full-page event response.</li>
<li><strong>accessible</strong> - Modify hyperlinks and form action urls to trigger server-side events without the use of javascript</li>
<li><strong>serialize</strong> - Form input CSS selector - sets the form or input elements to be submitted to the server when the event is trigger.</li>
<li><strong>script</strong> - (String/Array) - Javascript to be executed before event is sent to server. Use an associative array to assigned scripts that are to be executed before and after a callback. Example: array('before' => 'alert("Before Call")', 'after' => 'alert("After Call")')</li>
<li><strong>data</strong> - Data to be passed the to server-side event. This data is only passed to the event handler when the event is triggered. It is not sent to the client.</li>
<li><strong>autoDisable</strong> - Disables the selected elements during event transmission. Accepted values: TRUE | CSS Selectors. If TRUE is specified then the current element will be toggled</li>
<li><strong>autoToggle</strong> - Show/Hide selected elements during event transmission. Accepted values: TRUE | CSS-selectors. If TRUE is specified then the current element will be toggled</li>
<li><strong>delay</strong> - Delays server-side callback in milliseconds. Accepted values: TRUE | Integer. If TRUE is specifed then delay will be set to 200ms (minimum value)</li>
<li><strong>inputCache</strong> - Specifies the minimum number of input characters to cache before sending request to server. Possible values: TRUE or Interger - Number of characters to cache. This option is only valid for &lt;input type="text"&gt; and &lt;textarea&gt; elements.</li>
<li><strong>repeat</strong> - Number of times to repeat an ajax timeout event. Used internally by the timeout() method. Setting the value to TRUE will repeat the timeout indefinitely. Defaults to 1 for ajax timeouts.</li>
<li><strong>view</strong> - Set the view that should be called when the event is being invoked.</li>
<li><strong>confirm</strong> - Used to display a confirmation popup dialog before then event is triggered.</li>
</ul>

<h4><strong>Event Object</strong></h4>

<p>When the event handler is called, an instance of the <strong>RaxanWebPageEvent</strong> object is passed as the first parameter with the following properties:</p>

<ul>
<li><strong>target</strong> - Target DOMElement</li>
<li><strong>type</strong> - event type. Example: click</li>
<li><strong>value</strong> - value return from client. This value can either be assigned when binding to the event or it can be retrieved from the target element</li>
<li><strong>pageX</strong> - Mouse position X</li>
<li><strong>pageY</strong> - Mouse position Y</li>
<li><strong>targetX</strong> - Target element position X on client</li>
<li><strong>targetY</strong> - Target element position Y on client</li>
<li><strong>which</strong> - Ascii Key value</li>
<li><strong>button</strong> - Mouse button</li>
<li><strong>ctrlkey</strong> - Control Key</li>
<li><strong>metakey</strong> - Meta Key (Mac)</li>
<li><strong>uiDraggable</strong> - jQuery UI Draggable DOMElement</li>
<li><strong>uiHelper</strong> - jQuery UI Helper DOMElement</li>
<li><strong>uiSender</strong> - jQuery UI Sender DOMElement</li>
</ul>

<p>Available methods:</p>

<ul>
<li><strong>textVal</strong> - Returns text value after removing the html tags</li>
<li><strong>intVal</strong> - Returns an interger if value is numeric or null if there was an error</li>
<li><strong>floatVal</strong> - Returns float if value is numeric or null if there was an error.</li>
</ul>

<h3>Passing values from client to the Event Handler</h3>

<p>When an event is trigger a value is submit to the event handler. This can be useful when you want to carry out an action based on the returned value.</p>

<p>To do this you can either assign the value when binding the event to the DOM element:</p>

<pre><code class="php">&lt;?php
    protected function _init() {
        $this-&gt;button1-&gt;bind('click',array(
            'callback'=&gt;'.eventHandler',
            'value' =&gt; '12345'
        ));
    }

    protected function eventHandler($e) {
        // retrieve event value from the $e object
        $value = $e-&gt;value();

        // retrieve and sanitize event value
        $value = $e-&gt;intVal();

        /* do something here */
    }
?&gt;
</code></pre>

<p>Or set the value using one of the following methods:</p>

<p>If the element is an anchor tag, then add the value after the url "#" (hash):</p>

<pre><code>&lt;a href="#10" xt-bind="clic,changePage"&gt;Next Page&lt;/a&gt;
</code></pre>

<p>If the element is a form element then the content of the "<strong>value</strong>" attribute will be sent back to the server as the event value</p>

<pre><code>&lt;select name="select1" id="select1" xt-bind="change,doSomething"&gt;
    &lt;option value="1"&gt;Item 1&lt;/option&gt;
    &lt;option value="2"&gt;Item 2&lt;/option&gt;
&lt;/select&gt;
</code></pre>

<p>To set the event value on any element (including the above), you can use the "<strong>data-event-value</strong>" attribute or add the value to the "class" attribute.
When using the class attribute you must first add the "v:" prefix to the value as shown below:</p>

<pre><code>&lt;button id="btn1" 
    class="v:12345"
    xt-bind="click,showMessage"&gt;Submit&lt;/button&gt;
</code></pre>

<p>Or use the <strong>data-event-value</strong> attribute as follows:</p>

<pre><code>&lt;button id="btn2"
    data-event-value="12345"
    xt-bind="click,showMessage"&gt;Show Message&lt;/button&gt;
</code></pre>

<p><strong>Note:</strong> Adding values to the class attribute has being deprecated. Use the data-event-value attribute.</p>

<h4><strong>Additional data attributes</strong></h4>

<p>In addition to the the data-event-value attribute there are two other attributes that can be assigned to an element:</p>

<ul>
<li><p><strong>data-event-view</strong> - This is used to determine the view to loaded when the server-side event is being triggered.</p>

<pre><code>&lt;button id="btn1"
    data-event-view="page"
    xt-bind="click,changeView" &gt;Next Page&lt;/button&gt;
</code></pre></li>
<li><p><strong>data-event-confirm</strong> - This is used to display a confirmation dialog before then event is triggered.</p>

<pre><code>&lt;button id="btn1"
    data-event-confirm="Are you sure you want to delete this records?"
    xt-bind="click,deleteRow"&gt;Delete Record&lt;/button&gt;
</code></pre></li>
</ul>

<p>When the button is clicked, the value of the data-event-confirm attribute will be displayed as a confirmation popup before the request is sent
to the server.</p>

<h3>Registering Custom Server-Side Events</h3>

<p>There are times when you need to create special events for a page in order to execute a specific task. To do this you can you the registerEvent() method.</p>

<p>The following will register an event for the current page and bind the event to a callback handler:</p>

<pre><code class="php">&lt;?php
    protected function _init() {
        // registers a custom event
        $this-&gt;registerEvent('custom-event','.myEventHandler');
    }
?&gt;
</code></pre>

<p><strong>Note:</strong> Events must be registered before or during page load.</p>

<h3>Triggering a Server-Side Event</h3>

<p>Server-side events can be triggered by using the framework's API, client-side scripts, form submits or hyperlinks.</p>

<h4><strong>Using Hyperlinks to trigger events</strong></h4>

<p>Events can be triggered from a url or form by submitting special name/value pairs to the server via a POST or a GET method.</p>

<p>Here's a list of some of the name/value pairs that are used when making a GET request:</p>

<ul>
<li><strong>_e[target]</strong> - Target element id. For registered events the target id should be set to "page"</li>
<li><strong>_e[type]</strong> - Event Name - Defaults to click</li>
<li><strong>_e[value]</strong> - (Optional) Value to be passed to the event</li>
<li><strong>_e[tok]</strong> - Special token to be sent to server. This entry is optional if the event was registered using @global.</li>
</ul>

<p>Before you can trigger the event from a url you will have to either 1) bind the event as a global event using the <strong>@global</strong> keyword or 2) pass
a special token to the server along with the event request. Use the Raxan::$postBackToken property to retrieve the current token value.</p>

<p>Here's an example using <strong>@global</strong>:</p>

<pre><code class="php">&lt;?php
class NewPage extends RaxanWebPage {
    protected function _init() {
        // register editorder event as a global event
        $this-&gt;registerEvent('editorder@global','.editOrder');
    }

    protected function editOrder($e) {
        $v = $e-&gt;intVal();  // get event value
        /* do something here */
    }
}
?&gt;

&lt;a href="order.php?_e[target]=page&amp;_e[type]=editorder&amp;_e[value]=1234"&gt;Click here&lt;/a&gt;
</code></pre>

<p>The example above will register the <strong>editorder</strong> event as a global event so that it can be triggered from any url. This will also override the need
for passing the token to the event.</p>

<p>Another solution is to let the framework handle the request gracefully. This is useful when JavaScript is not enabled inside the web browser:</p>

<pre><code class="php">&lt;?php
    protected function _config() {
        $this-&gt;degradable = true; //enables degradable mode if JavaScript is not available
    }

    protected function _init() {
        $this-&gt;button1-&gt;bind('click','.editOrder');
    }
?&gt;

&lt;a id="button1" href="#1234"&gt;Edit Order&lt;/a&gt;
</code></pre>

<p>In the above example, the framework will automatically generate the necessary code to trigger the event when JavaScript is not available.</p>

<p><strong>Note:</strong>  Raxan has a built-in security feature that helps to prevent Cross-Site Request Forgery (CSRF). Each event request must be accompanied
with a valid token id. This token is unique to the user's active browser session and will be destroyed once the browser is closed. To bypass
this security check you must append the <strong>@global</strong> to the event name when binding to or registering the event.</p>

<h4><strong>Using JavaScript to trigger events</strong></h4>

<p>In addition to hyperlinks, you can programmatically trigger a server-side event by using either the Raxan.dispatchEvent() method or manually triggering
the event on the browser's DOM element:</p>

<pre><code class="javascript">    &lt;script type="text/javascript"&gt;
        function dispatchEventExamples() {

            // manually trigger button1 click event
            $('#button1').trigger('click'); // this will also trigger the click event on the server

            // trigger button1 click event (normal post back)
            Raxan.dispatchEvent({
                type: 'click',          // event name
                target: 'button1'       // target element
            })

            // explicitly trigger button1 click event via ajax
            Raxan.dispatchEvent({
                type: '#click',             // event name
                target: 'button1'
            })

            // implicitly trigger click event via ajax by adding a callback function
            Raxan.dispatchEvent({
                type: 'click',          // event name
                target: 'button1',
                complete: function(result,success){
                    if (!success) return;
                    if (result=='ok') alert('Task completed');
                }
            })

            // trigger button2 "click" event on another page
            Raxan.dispatchEvent({
                type: 'click',              // event name
                target: 'button2',
                url:  'another-page.php'   // page url where event is registered
            })


            // passing a value to the event
            Raxan.dispatchEvent({
                type: 'click',          // event name
                target: 'button1',
                value: 'This is a messge from the browser',
                complete: function(result,success){
                    if (!success) return;
                    // do something here
                }
            })

            // serialize form elements
            Raxan.dispatchEvent({
                type: 'click',          // event name
                target: 'button1',
                serialize: '#form1',    // css selector
                complete: function(result,success){
                    if (!success) {
                        // handle error
                        alert('Error while connecting to server');
                        return true;  // let the Raxan know that we that the error was handled
                    }
                    // do something here
                }
            })

        }
    &lt;/script&gt;
</code></pre>

<p>To trigger a custom (registered) event from the client, use the dispatchEvent() method as shown below:</p>

<pre><code class="javascript">&lt;script type="text/javascript"&gt;
    function dispatchEventExamples() {
         // trigger "custom" event (normal post back)
        Raxan.dispatchEvent('custom-event');

        // implicitly trigger "custom" event via ajax by passing a callback function
        Raxan.dispatchEvent('custom-event',function(result,success){
            if (!success) return;
            // do something
        });

        // passing a value to the custom event
        Raxan.dispatchEvent('custom-event',value,function(result,success){
            if (!success) return;
            // do something
        });
    }
&lt;/script&gt;
</code></pre>

<p>The <strong>dispatchEvent()</strong> method is primarily to use to invoke custom (registered) events. See <a href="client-server.html">Client-Server Introduction</a>
for more information.</p>

<h3>Intercepting Events</h3>

<p>It's possible to intercept and prevent a server-side event from being triggered by listening to special events on the target element. This feature can
be useful when you need to perform an action before or after the event has been triggered.</p>

<p>To do this you can use one or more of the following client-side events:</p>

<ul>
<li><strong>togglecontent</strong> - This event is similar in function to the autotoggle parameter of "xt-bind" and "xt-delgate" attributes. It can be used to toggle preloaders
or validate user data.</li>
<li><strong>disablecontent</strong> - This event is similar in function to the autodisable parameter of the "xt-bind" and "xt-delgate" attributes. It can be used to disable elements
before the data is sent to the server.</li>
<li><strong>serializecontent</strong> - This event is similar in function to the serialize parameter of the "xt-bind" and "xt-delgate" attributes. It can be used to serialize
data to be submitted to the server.</li>
</ul>

<h4><strong>The <em>togglecontent</em> event</strong></h4>

<p>This event is invoked before and after an event notification is sent to the server. This can be useful when you want to validate form data  or
toggle special pre-loader elements:</p>

<pre><code class="javascript">&lt;script type="text/javascript"&gt;
    Raxan.ready(function(){
        $('#buton1').bind('togglecontent',function(e,mode){
            if (mode=='on') {       // before server-side event execution
                var valid = true;
               // code to validate form data
               if (!valid) return false; // return false to cancel event execution

               // show pre-loader
               $('#preloader').show();
            }
            else if (mode=='off') { // after server-side event execution
                // hide pre-loader
                $('#preloader').hide();

                // get data returned from server-side event handler
                var result = e.serverResult;
                // do something with result
            }
        })
    })
&lt;/script&gt;
</code></pre>

<p><strong>Note:</strong> If the "autotoggle" selector option or parameter was assigned to the element on the server, then the "autotoggle" elements will be toggled
before this event is invokded.</p>

<h4><strong>The <em>disablecontent</em> event</strong></h4>

<p>This event is invoked before and after an event notification is sent to the server.</p>

<pre><code class="javascript">&lt;script type="text/javascript"&gt;
    Raxan.ready(function(){
        $('#buton1').bind('disablecontent',function(e,mode){
            if (mode=='on') {       // before server-side event execution
                var valid = true;
               // code to validate form data
               if (!valid) return false; // return false to cancel event execution

               // disable form elements
               $('#form :input').attr('disabled','disabled');
            }
            else if (mode=='off') { // after server-side event execution
                // enable form elements
                $('#form :input').removeAttr('disabled');

                // get data returned from server-side event handler
                var result = e.serverResult;
                // do something with result
            }
        })
    })
&lt;/script&gt;
</code></pre>

<p><strong>Note:</strong> If the "autodisable" selector option or parameter was assigned to the element on the server, then the "autodisable" elements will be disabled/enabled before this event
is invokded.</p>

<h4>The <em>serializecontent</em> event</h4>

<p>This event is invoked before an event notification is sent to the server. The event handler must return a 2D array containing data to be
submitted to the server.</p>

<pre><code class="javascript">&lt;script type="text/javascript"&gt;
    Raxan.ready(function(){
        $('#buton1').bind('serializecontent',function(e){
            var data = $('#form').serializeArray();
            // add additional data to be submitted to server
            data[data.length] = {name:'any-name', value:'any-value'}
            return data;
        })
    })
&lt;/script&gt;
</code></pre>

<p><strong>Note:</strong> If the "serialize" selector option or parameter was assigned to the element on the server, then the data from the "serialize" elements will be
merged with data returned from the <strong>serializecontent</strong> event.</p>

<hr class="clear" />

<p align="right">Up Next: <a href="templates.html" title="Server-Side Templates">Server-Side Templates</a> </p>
</div>
            
            <div id="footer" class="container c48 rax-active-pal round rax-glossy">
                <ul class="clearfix">
                    <li><a href="index.html">Overview</a></li>
                    <li><a href="features.html">Features</a></li>
                    <li><a href="new-features.html">What's new</a></li>
                    <li><a href="table-of-contents.html" title="Table of Content">Contents</a></li>
                    <li><a href="../examples">Examples</a></li>
                </ul>
            </div>
        </div>
    </div>
</body>

</html>

